﻿using System;

using EHTWebsiteProject.Code.BLL;

using EHTWebsiteProject.Code.Entity;
using EHTWebsite.Code;

using EHTWebsite.Code;


namespace EHTWebsiteProject.Code.Entity
{
    /**
     * The CartItem Class
     * 
     * Basically a structure for holding item data
     */
    public class CartItem : IEquatable<CartItem>
    {
        #region Properties

        // A place to store the quantity in the cart
        // This property has an implicit getter and setter.
        public float Quantity { get; set; }

        private int _productId;
        public int ProductId
        {
            get { return _productId; }
            set
            {
                // To ensure that the Prod object will be re-created
                _product = null;
                _productId = value;
            }
        }

        private SanPham _product = null;
        public SanPham Prod
        {
            get
            {
                // Lazy initialization - the object won't be created until it is needed
                if (_product == null)
                {
                    _product = new SanPham();
                    _product = SanPhamProvider.GetSanPhamBySanPhamID(_productId);
                }
                return _product;
            }
        }

        public string Description
        {

            get {
                String str = Prod.TenSanPhamVN;
                return str;
            }

        }

        public float UnitPrice
        {
            get { return Prod.DonGia; }
        }

        public float TotalPrice
        {
            get { return UnitPrice * Quantity; }
        }

        #endregion

        // CartItem constructor just needs a productId
        public CartItem(int productId)
        {
            this.ProductId = productId;
        }

        /**
         * Equals() - Needed to implement the IEquatable interface
         *    Tests whether or not this item is equal to the parameter
         *    This method is called by the Contains() method in the List class
         *    We used this Contains() method in the ShoppingCart AddItem() method
         */
        public bool Equals(CartItem item)
        {
            return item.ProductId == this.ProductId;
        }
    }
}